package com.gomore.opple.module.award.unlottery;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.gomore.opple.R;
import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.model.EventActionFinish;
import com.gomore.opple.model.EventReflashLotteriedFragment;
import com.gomore.opple.module.BaseFragmentV4;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.module.award.adapter.AwardAdapter;
import com.gomore.opple.service.DrawAwardClickListener;
import com.gomore.opple.utils.DialogUtils;
import com.gomore.opple.web.cgform.lottery.entity.TODrawEntity;
import com.gomore.opple.widgets.PullBaseView;
import com.gomore.opple.widgets.PullRecyclerView;

import butterknife.Bind;
import de.greenrobot.event.EventBus;


/**
 * Created by robot
 */
public class UnlotteryFragment extends BaseFragmentV4 implements UnlotteryContract.View,
        PullBaseView.OnRefreshListener, DrawAwardClickListener {

    @Bind(R.id.recyclerView)
    PullRecyclerView recyclerView;
    @Bind(R.id.empty_view)
    View empty_view;
    private UnlotteryContract.Presenter mPresenter;
    private AwardAdapter awardAdapter;

    public static UnlotteryFragment getInstance() {
        return new UnlotteryFragment();
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_unlottery;
    }

    @Override
    protected void initalizeData() {
        EventBus.getDefault().register(this);
        mPresenter.queryAward(false);
    }

    @Override
    protected void initalizeViews() {
        awardAdapter = new AwardAdapter(getActivity(), getActivity(), R.layout.lottery_item, mPresenter.getData(), GlobalConstant.AwardState.INITIAL);
        recyclerView.setLayoutManager(new LinearLayoutManager(UnlotteryFragment.this.getActivity()));
        recyclerView.setAdapter(awardAdapter);
        recyclerView.setOnRefreshListener(this);
        awardAdapter.setDrawAwardClickListener(this);
    }

    @Override
    public void setPresenter(UnlotteryContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showProgressDialog() {
        DialogUtils.showLoadingDialog(getActivity(), getResources().getString(R.string.loading));
    }

    @Override
    public void hideProgressDialog() {
        DialogUtils.closeLoadingDialog();
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void showMessage(int msgId) {
        showToast(msgId);
    }

    /**
     * 加载完毕
     */
    @Override
    public void showLoadMoreCompleted() {
        recyclerView.onFooterRefreshComplete();
    }

    /**
     * 刷新完毕
     */
    @Override
    public void showRefreshCompleted() {
        recyclerView.onHeaderRefreshComplete();
    }

    @Override
    public void showContentView() {
        awardAdapter.notifyDataSetChanged();
        if (mPresenter.getData().size() == 0) {
            empty_view.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            empty_view.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onHeaderRefresh(PullBaseView view) {
        mPresenter.queryAward(false);
    }

    @Override
    public void onFooterRefresh(PullBaseView view) {
        mPresenter.queryAward(true);
    }

    @Override
    public void drawAward(TODrawEntity drawEntity) {
        if (drawEntity != null) {
            IntentStart.getInstance().startLotteryActivity(getActivity(), drawEntity.getLotteryId(), drawEntity.getOrderNumber(), drawEntity.getId(), GlobalConstant.FragmentType.UNLOTTERYFRAGMENT);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //抽奖结果中断
    public void onEventMainThread(EventActionFinish eventActionFinish) {
        if (eventActionFinish != null && !eventActionFinish.isFinish() &&
                eventActionFinish.getType().equals(GlobalConstant.FragmentType.UNLOTTERYFRAGMENT)) {
            //抽奖之后刷新当前页面，和已抽奖页面
            mPresenter.queryAward(false);
            EventReflashLotteriedFragment eventReflashLotteriedFragment = new EventReflashLotteriedFragment();
            eventReflashLotteriedFragment.setReflash(true);
            EventBus.getDefault().post(eventReflashLotteriedFragment);
        }
    }
}
